package jp.idahopotato.activity;

import java.util.ArrayList;
import java.util.List;

import jp.idahopotato.adapter.ResultSetAdapter;
import jp.idahopotato.aidl.ResultListAIDL;
import jp.idahopotato.aidl.SearchServiceAIDL;
import jp.idahopotato.bean.Result;
import jp.matome.activity.R;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ResultListActivity extends ListActivity {
	private static ListView lv = null;
	CreateProductHelper helper = null;
	SQLiteDatabase db = null;
	private SearchServiceAIDL bindserviceIf = null;
	private ServiceConnection conn = null;

	//検索サービスからの通知を受け取る
	private ResultListAIDL bindactivityIf = new ResultListAIDL.Stub() {
		public void addResult(Result rs) throws RemoteException {
			handler.sendMessage(Message.obtain(handler, 0, rs));
		}
	};
	private Handler handler = new Handler(){
		public void handleMessage(Message msg){
			Result rs = (Result) msg.obj;
			@SuppressWarnings({ "unchecked" })
			//結果の追加
			ArrayAdapter<Result> temp = (ArrayAdapter<Result>)lv.getAdapter();
			temp.insert(rs, temp.getCount());
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resultlist);

		// 検索サービスの開始
		Toast.makeText(ResultListActivity.this, "検索を開始します", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(SearchServiceAIDL.class.getName());
		conn = new ServiceConnection() {
			public void onServiceDisconnected(ComponentName name) {
				bindserviceIf = null;
			}

			public void onServiceConnected(ComponentName compName, IBinder binder) {
				bindserviceIf = SearchServiceAIDL.Stub.asInterface(binder);
				try{
					bindserviceIf.registerCallback(bindactivityIf);
				} catch(RemoteException e){
					Log.e("ERROR", e.getMessage());
				}
			}
		};
		bindService(intent, conn, BIND_AUTO_CREATE);

		//結果リストの初期化
		List<Result> list = new ArrayList<Result>();
		ResultSetAdapter adapter = new ResultSetAdapter(ResultListActivity.this, list);
		setListAdapter(adapter);
		lv = getListView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		try{
			bindserviceIf.unregisterCallback(bindactivityIf);
		} catch(RemoteException e){
			Log.e("ERROR", e.getMessage());
		}
		Toast.makeText(ResultListActivity.this, "検索を終了します", Toast.LENGTH_SHORT).show();
		unbindService(conn);
		Intent intent = new Intent(SearchServiceAIDL.class.getName());
		stopService(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data != null && data.hasExtra("RETURN_FLAG")){

		}
	}
}
